package com.op.teachingliteapp.sender.ui.view.zoomgallery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;

/**
 * Created by Genji on 2015/6/29.
 */
public class ZoomImageView extends ImageView {
    static final float SCALE_RATE = 1.25F;
    float _dx = 0.0F;
    float _dy = 0.0F;
    int centerCounter = 0;
    int currentAlpha = 0;
    float dx = 0.0F;
    int flingFactor = 2;
    protected Bitmap image = null;
    private int imageHeight;
    private int imageWidth;
    private boolean isLoad = false;
    protected Matrix mBaseMatrix = new Matrix();
    private final Matrix mDisplayMatrix = new Matrix();
    private int mGalleryHeight = 0;
    private int mGalleryWidth = 0;
    protected Handler mHandler = new Handler();
    private final float[] mMatrixValues = new float[9];
    float mMaxZoom = 3.0F;
    float mMinZoom;
    protected Matrix mSuppMatrix = new Matrix();
    int mThisHeight = -1;
    int mThisWidth = -1;
    float newDx = 0.0F;
    float newDy = 0.0F;
    private float scaleRate;
    boolean stop = false;

    public ZoomImageView(Context var1, int var2, int var3) {
        super(var1);
        this.mGalleryWidth = var2;
        this.mGalleryHeight = var3;
        this.init();
    }

    public ZoomImageView(Context var1, AttributeSet var2) {
        super(var1, var2);
        this.init();
    }

    private void arithScaleRate() {
        this.scaleRate = Math.min((float) this.mGalleryWidth / (float) this.imageWidth, (float) this.mGalleryHeight / (float) this.imageHeight);
        this.mMaxZoom = 3.0F;
        this.mMaxZoom *= this.scaleRate;
        this.mMinZoom = this.scaleRate;
    }

    private void init() {
        this.setScaleType(ScaleType.MATRIX);
        this.center(true, true);
    }

    private void printImageInfo() {
        float[] var1 = new float[9];
        this.getImageMatrix().getValues(var1);
        float var2 = var1[2];
        float var3 = var1[5];

    }

    protected void center(boolean var1, boolean var2) {
        if (this.image != null) {
            Matrix var3 = this.getImageViewMatrix();
            RectF var4 = new RectF(0.0F, 0.0F, (float) this.image.getWidth(), (float) this.image.getHeight());
            var3.mapRect(var4);
            float var6 = var4.height();
            float var7 = var4.width();
            float var8;
            if (this.centerCounter != 0) {
                var8 = 0.0F;
                if (var2) {
                    int var12 = this.getHeight();
                    if (var6 < (float) var12) {
                        var8 = ((float) var12 - var6) / 2.0F - var4.top;
                    } else if (var4.top > 0.0F) {
                        var8 = -var4.top;
                    } else if (var4.bottom < (float) var12) {
                        var8 = (float) this.getHeight() - var4.bottom;
                    } else {
                        float var16;
                        int var13 = (var16 = var4.top - 0.0F) == 0.0F ? 0 : (var16 < 0.0F ? -1 : 1);
                        var8 = 0.0F;
                        if (var13 < 0) {
                            int var14 = this.centerCounter;
                            var8 = 0.0F;
                            if (var14 == 0) {
                                var8 = -var4.top;
                            }
                        }
                    }
                }
            } else {
                int var15 = this.centerCounter;
                var8 = 0.0F;
                if (var15 == 0) {
                    var8 = 0.0F - var4.top + ((float) this.mGalleryHeight - (float) this.image.getHeight() * this.getBasedScaleRate()) / 2.0F;
                }
            }

            float var9 = 0.0F;
            if (var1) {
                int var10 = this.getWidth();
                if (this.centerCounter == 0) {
                    var9 = 0.0F - var4.left + ((float) this.mGalleryWidth - (float) this.image.getWidth() * this.getBasedScaleRate()) / 2.0F;
                } else if (var7 < (float) var10) {
                    var9 = ((float) var10 - var7) / 2.0F - var4.left;
                } else if (var4.left > 0.0F) {
                    var9 = -var4.left;
                } else {
                    float var17;
                    int var11 = (var17 = var4.right - (float) var10) == 0.0F ? 0 : (var17 < 0.0F ? -1 : 1);
                    var9 = 0.0F;
                    if (var11 < 0) {
                        var9 = (float) var10 - var4.right;
                    }
                }

                ++this.centerCounter;
            }

            this.postTranslate(var9, var8);
            this.setImageMatrix(this.getImageViewMatrix());
        }
    }

    public float getBasedScaleRate() {
        return this.scaleRate;
    }

    public Bitmap getBitmap() {
        return this.image;
    }

    public Bitmap getCopyBitamp(Bitmap.Config var1) {
        return this.image.copy(var1, true);
    }

    public int getImageHeight() {
        return this.imageHeight;
    }

    protected Matrix getImageViewMatrix() {
        this.mDisplayMatrix.set(this.mBaseMatrix);
        this.mDisplayMatrix.postConcat(this.mSuppMatrix);
        return this.mDisplayMatrix;
    }

    public int getImageWidth() {
        return this.imageWidth;
    }

    public boolean getLoad() {
        return this.isLoad;
    }

    protected float getScale() {
        return this.getScale(this.mSuppMatrix);
    }

    protected float getScale(Matrix var1) {
        return this.getValue(var1, 0);
    }

    protected float getValue(Matrix var1, int var2) {
        var1.getValues(this.mMatrixValues);
        this.mMinZoom = (float) this.mGalleryWidth / 2.0F / (float) this.imageWidth;
        return this.mMatrixValues[var2];
    }

    public void layoutToCenter() {
        float var1 = (float) this.imageWidth * this.getScale();
        float var2 = (float) this.imageHeight * this.getScale();
        float var3 = (float) this.mGalleryWidth - var1;
        float var4 = (float) this.mGalleryHeight - var2;
        float var9;
        int var5 = (var9 = var3 - 0.0F) == 0.0F ? 0 : (var9 < 0.0F ? -1 : 1);
        float var6 = 0.0F;
        if (var5 > 0) {
            var6 = var3 / 2.0F;
        }

        float var10;
        int var7 = (var10 = var4 - 0.0F) == 0.0F ? 0 : (var10 < 0.0F ? -1 : 1);
        float var8 = 0.0F;
        if (var7 > 0) {
            var8 = var4 / 2.0F;
        }

        this.postTranslate(var6, var8);
        this.setImageMatrix(this.getImageViewMatrix());
    }

    protected float maxZoom() {
        return this.image == null ? 1.0F : 4.0F * Math.max((float) this.image.getWidth() / (float) this.mThisWidth, (float) this.image.getHeight() / (float) this.mThisHeight);
    }


    protected void onDraw(Canvas var1) {
        try {
            super.onDraw(var1);
        } catch (Exception var3) {

        }
    }

    public boolean onKeyDown(int var1, KeyEvent var2) {
        if (var1 == 4 && var2.getRepeatCount() == 0) {
            var2.startTracking();
            return true;
        } else {
            return super.onKeyDown(var1, var2);
        }
    }

    public boolean onKeyUp(int var1, KeyEvent var2) {
        if (var1 == 4 && var2.isTracking() && !var2.isCanceled() && this.getScale() > 1.0F) {
            this.zoomTo(1.0F);
            return true;
        } else {
            return super.onKeyUp(var1, var2);
        }
    }

    protected void panBy(float var1, float var2) {
        this.postTranslate(var1, var2);
        this.setImageMatrix(this.getImageViewMatrix());
    }

    public void postTranslate(float var1, float var2) {
        this.mSuppMatrix.postTranslate(var1, var2);
        this.setImageMatrix(this.getImageViewMatrix());
    }

    protected void postTranslateDur(float var1, final float var2) {
        this._dy = 0.0F;
        final float var3 = var1 / var2;
        final long var4 = System.currentTimeMillis();
        this.mHandler.post(new Runnable() {
            public void run() {
                long var1 = System.currentTimeMillis();
                float var3x = Math.min(var2, (float) (var1 - var4));
                ZoomImageView.this.postTranslate(0.0F, var3x * var3 - ZoomImageView.this._dy);
                ZoomImageView.this._dy = var3x * var3;
                if (var3x < var2) {
                    ZoomImageView.this.mHandler.post(this);
                }

            }
        });
    }

    void postTranslateXY(float var1, float var2) {
        this._dx = var1;
        this._dy = var2;
        this.newDx = 0.0F;
        this.newDy = 0.0F;
        this.flingFactor = 2;
        this.stop = false;
        this.mHandler.post(new Runnable() {
            public void run() {
                ZoomImageView var1 = ZoomImageView.this;
                var1._dx /= (float) ZoomImageView.this.flingFactor;
                ZoomImageView var2 = ZoomImageView.this;
                var2._dy /= (float) ZoomImageView.this.flingFactor;
                float var3 = ZoomImageView.this.getScale() * (float) ZoomImageView.this.getImageWidth();
                float var4 = ZoomImageView.this.getScale() * (float) ZoomImageView.this.getImageHeight();
                float[] var5 = new float[9];
                ZoomImageView.this.getImageMatrix().getValues(var5);
                float var6 = var5[5];
                float var7 = var5[2];
                float var8 = var6 + var4;
                float var9 = var7 + var3;
                if (var9 + ZoomImageView.this._dx < (float) ZoomImageView.this.mGalleryWidth) {
                    ZoomImageView.this.newDx = (float) ZoomImageView.this.mGalleryWidth - var9;
                    ZoomImageView.this.flingFactor = 8;
                } else if (var7 + ZoomImageView.this._dx > 0.0F) {
                    ZoomImageView.this.newDx = -var7;
                    ZoomImageView.this.flingFactor = 8;
                }

                if (var8 + ZoomImageView.this._dy < (float) ZoomImageView.this.mGalleryHeight) {
                    ZoomImageView.this.newDy = (float) ZoomImageView.this.mGalleryHeight - var8;
                    ZoomImageView.this.flingFactor = 8;
                } else if (var6 + ZoomImageView.this._dy > 0.0F) {
                    ZoomImageView.this.newDy = -var6;
                    ZoomImageView.this.flingFactor = 8;
                }

                if (Math.abs(ZoomImageView.this._dx) <= 1.0F && Math.abs(ZoomImageView.this._dy) <= 1.0F) {
                    if (ZoomImageView.this.newDx != 0.0F || ZoomImageView.this.newDy != 0.0F) {
                        ZoomImageView.this.stop = true;
                        ZoomImageView.this.postTranslateXYDur(ZoomImageView.this.newDx, ZoomImageView.this.newDy, 200.0F);
                        return;
                    }
                } else {
                    ZoomImageView.this.postTranslate(ZoomImageView.this._dx, ZoomImageView.this._dy);
                    ZoomImageView.this.mHandler.postDelayed(this, 50L);
                }

            }
        });
    }

    protected void postTranslateXYDur(float var1, float var2, final float var3) {
        this._dx = 0.0F;
        this._dy = 0.0F;
        final float var4 = var1 / var3;
        final float var5 = var2 / var3;
        final long var6 = System.currentTimeMillis();
        this.mHandler.post(new Runnable() {
            public void run() {
                long var1 = System.currentTimeMillis();
                float var3x = Math.min(var3, (float) (var1 - var6));
                ZoomImageView.this.postTranslate(0.0F, var3x * var4 - ZoomImageView.this._dx);
                ZoomImageView.this.postTranslate(0.0F, var3x * var5 - ZoomImageView.this._dy);
                ZoomImageView.this._dx = var3x * var4;
                ZoomImageView.this._dy = var3x * var5;
                if (var3x < var3) {
                    ZoomImageView.this.mHandler.post(this);
                }

            }
        });
    }

    protected void runAlphaAnimation(final float var1) {
        final float var2 = (float) (255 - this.currentAlpha) / var1;
        final int var3 = this.currentAlpha;
        final long var4 = System.currentTimeMillis();
        this.mHandler.post(new Runnable() {
            public void run() {
                long var1x = System.currentTimeMillis();
                float var3x = Math.min(var1, (float) (var1x - var4));
                int var4x = (int) ((float) var3 + var3x * var2);
                ZoomImageView.this.currentAlpha = var4x;
                if (var3x < var1) {
                    ZoomImageView.this.setAlpha(var4x);
                    ZoomImageView.this.mHandler.post(this);
                } else {
                    ZoomImageView.this.setAlpha(255);
                }
            }
        });
    }

    public void setImageBitmap(Bitmap var1) {
        super.setImageBitmap(var1);
        this.centerCounter = 0;
        this.image = var1;

//
        if (this.image != null) {
            this.imageHeight = this.image.getHeight();
            this.imageWidth = this.image.getWidth();
            this.mBaseMatrix.setTranslate(0.0F, 0.0F);
            this.mSuppMatrix.setTranslate(0.0F, 0.0F);
            this.mDisplayMatrix.setTranslate(0.0F, 0.0F);
            this.arithScaleRate();
            this.zoomTo(this.getBasedScaleRate(), (float) this.mGalleryWidth / 2.0F, (float) this.mGalleryHeight / 2.0F, true);
        }

    }

    public void setImageHeight(int var1) {
        this.imageHeight = var1;
    }

    public void setImageWidth(int var1) {
        this.imageWidth = var1;
    }

    public void setLoad(boolean var1) {
        this.isLoad = var1;
    }

    public void switchImageBitmap(Bitmap var1) {
        float[] var2 = new float[9];
        this.getImageMatrix().getValues(var2);
        float var3 = var2[2];
        float var4 = var2[5];
        float var5 = 1.0F;
        if (this.image != null) {
            var5 = (float) this.image.getWidth() / (float) var1.getWidth();
            Log.i("AWSENDER", "before switch picture width = " + (float) this.image.getWidth() * this.getScale(this.mSuppMatrix) + " height = " + (float) this.image.getHeight() * this.getScale(this.mSuppMatrix) + " X = " + var3 + " Y =" + var4);
            this.image.recycle();
        }

        float var6 = var5 * this.getScale(this.mSuppMatrix);
        super.setImageBitmap(var1);
        this.image = var1;
        this.imageHeight = this.image.getHeight();
        this.imageWidth = this.image.getWidth();
        Log.v("AWSENDER", "switch image to " + this.imageWidth + " * " + this.imageHeight);
        this.arithScaleRate();
        this.mBaseMatrix.setTranslate(0.0F, 0.0F);
        this.mSuppMatrix.setTranslate(0.0F, 0.0F);
        this.mDisplayMatrix.setTranslate(0.0F, 0.0F);
        this.zoomAndMoveTo(var6, var3, var4);
    }

    protected void zoomAndMoveTo(float var1, float var2, float var3) {
        if (var1 > this.mMaxZoom) {
            var1 = this.mMaxZoom;
        } else if (var1 < this.scaleRate) {
            var1 = this.scaleRate;
        }

        float var4 = var1 / this.getScale();
        this.mSuppMatrix.postScale(var4, var4);
        this.setImageMatrix(this.getImageViewMatrix());
        float[] var6 = new float[9];
        this.getImageMatrix().getValues(var6);
        float var7 = var6[2];
        float var8 = var6[5];
        float var9 = var2 - var7;
        float var10 = var3 - var8;
        this.mSuppMatrix.postTranslate(var9, var10);
        this.setImageMatrix(this.getImageViewMatrix());
    }

    protected void zoomIn() {
        this.zoomIn(1.25F);
    }

    protected void zoomIn(float var1) {
        Log.i("qvod", "zoomIn rate = " + var1);
        if (this.getScale() < this.mMaxZoom && this.getScale() > this.mMinZoom && this.image != null) {
            float var3 = (float) this.getWidth() / 2.0F;
            float var4 = (float) this.getHeight() / 2.0F;
            this.mSuppMatrix.postScale(var1, var1, var3, var4);
            this.setImageMatrix(this.getImageViewMatrix());
        }
    }

    protected void zoomOut() {
        this.zoomOut(1.25F);
    }

    protected void zoomOut(float var1) {
        Log.i("qvod", "zoomOut rate = " + var1);
        if (this.image != null) {
            float var3 = (float) this.getWidth() / 2.0F;
            float var4 = (float) this.getHeight() / 2.0F;
            Matrix var5 = new Matrix(this.mSuppMatrix);
            var5.postScale(1.0F / var1, 1.0F / var1, var3, var4);
            if (this.getScale(var5) < 1.0F) {
                this.mSuppMatrix.setScale(1.0F, 1.0F, var3, var4);
            } else {
                this.mSuppMatrix.postScale(1.0F / var1, 1.0F / var1, var3, var4);
            }

            this.setImageMatrix(this.getImageViewMatrix());
            this.center(true, true);
        }
    }

    protected void zoomTo(float var1) {
        this.zoomTo(var1, (float) this.getWidth() / 2.0F, (float) this.getHeight() / 2.0F, true);
    }

    protected void zoomTo(float var1, final float var2, final float var3, final float var4) {
        final float var5 = (var1 - this.getScale()) / var4;
        final float var6 = this.getScale();
        final float var7 = (float) (255 - this.currentAlpha) / var4;
        final int var8 = this.currentAlpha;
        final long var9 = System.currentTimeMillis();
        this.mHandler.post(new Runnable() {
            public void run() {
                long var1 = System.currentTimeMillis();
                float var3x = Math.min(var4, (float) (var1 - var9));
                float var4x = var6 + var3x * var5;
                int var5x = (int) ((float) var8 + var3x * var7);
                ZoomImageView.this.currentAlpha = var5x;
                ZoomImageView.this.zoomTo(var4x, var2, var3, true);
                if (var3x < var4) {
                    ZoomImageView.this.mHandler.post(this);
                }

            }
        });
    }

    protected void zoomTo(float var1, float var2, float var3, boolean var4) {
        if (var1 > this.mMaxZoom) {
            var1 = this.mMaxZoom;
        } else if (var1 < this.scaleRate) {
            var1 = this.scaleRate;
        }

        float var5 = var1 / this.getScale();
        this.mSuppMatrix.postScale(var5, var5, var2, var3);
        this.setImageMatrix(this.getImageViewMatrix());
        if (var4) {
            this.center(true, true);
        }

    }

    protected void zoomToPoint(float var1, float var2, float var3) {
        float var4 = (float) this.getWidth() / 2.0F;
        float var5 = (float) this.getHeight() / 2.0F;
        this.panBy(var4 - var2, var5 - var3);
        this.zoomTo(var1, var4, var5, true);
    }
}
